@extends('back.common.layout')
@section('title', '配置项列表')

@section('content')
    <div id="content">
        <div class="page-header">
            <div class="container-fluid">
                <div class="pull-right">
                    <button type="submit" form="form-setting" data-toggle="tooltip" title="保存" class="btn btn-primary">
                        <i class="fa fa-save"></i>
                    </button>

                    <a href="#" data-toggle="tooltip" title="取消" class="btn btn-default">
                        <i class="fa fa-reply"></i>
                    </a>
                </div>
                <h1>商品配置项</h1>
                <ul class="breadcrumb">
                    <li>
                        <a href="#">首页</a>
                    </li>
                    <li>
                        <a href="{{route('setting.list')}}">配置项列表</a>
                    </li>
                </ul>
            </div>
        </div>
        <div class="container-fluid">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <i class="fa fa-list"></i>
                        商品配置项
                    </h3>
                </div>
                <div class="panel-body">
                    <form action="{{route('setting.set')}}" method="post" enctype="multipart/form-data"
                          id="form-setting" class="form-horizontal">
                        {{ csrf_field() }}
                        <ul class="nav nav-tabs">
                            @foreach ($settingGroupRow as $settingGroup)
                                <li class="@if($loop->iteration == 1) active @endif">
                                    <a href="#tab-group-{{$settingGroup->id}}" data-toggle="tab">{{$settingGroup->title}}</a>
                                </li>
                            @endforeach
                        </ul>

                        <div class="tab-content">
                            @foreach ($settingGroupList as $key => $settingGroup)
                                <div class="tab-pane @if($loop->iteration == 1) active @endif" id="tab-group-{{$key}}">
                                    @foreach ($settingGroup as $setting)
                                        <div class="form-group required">
                                            <label class="col-sm-2 control-label" for="input-{{$setting['key']}}">{{$setting['title']}}</label>
                                            <div class="col-sm-9">
                                                @if($setting['type_title'] == 'text')
                                                    <input type="text" name="setting[{{$setting['id']}}]" value="{{$setting['value']}}" placeholder="{{$setting['title']}}" id="input-{{$setting['key']}}" class="form-control"/>
                                                    @if ($errors->has("setting.{$setting['id']}"))
                                                        <label for="input-title" class="text-danger">{{$errors->first("setting.{$setting['id']}")}}</label>
                                                    @endif
                                                @elseif($setting['type_title'] == 'textarea')
                                                    <textarea row="5" name="setting[{{$setting['id']}}]" placeholder="{{$setting['title']}}" id="input-{{$setting['key']}}" class="form-control">{{$setting['value']}}</textarea>
                                                    @if ($errors->has("setting.{$setting['id']}"))
                                                        <label for="input-title" class="text-danger">{{$errors->first("setting.{$setting['id']}")}}</label>
                                                    @endif
                                                @elseif($setting['type_title'] == 'select')
                                                    <select name="setting[{{$setting['id']}}]" id="select-{{$setting['key']}}" class="form-control">
                                                        @foreach ($setting['optionList'] as $option)
                                                            <option @if($option['id'] == $setting['value']) selected @endif value="{{$option['id']}}">{{$option['title']}}</option>
                                                        @endforeach
                                                    </select>
                                                    @if ($errors->has("setting.{$setting['id']}"))
                                                        <label for="input-title" class="text-danger">{{$errors->first("setting.{$setting['id']}")}}</label>
                                                    @endif
                                                @elseif($setting['type_title'] == 'select-multi')
                                                    <select multiple name="setting[{{$setting['id']}}][]" id="select-{{$setting['key']}}" class="form-control">
                                                        @foreach ($setting['optionList'] as $option)
                                                            <option @if(in_array($option['id'], explode(',', $setting['value']))) selected @endif value="{{$option['id']}}">{{$option['title']}}</option>
                                                        @endforeach
                                                    </select>
                                                    @if ($errors->has("setting.{$setting['id']}"))
                                                        <label for="input-title" class="text-danger">{{$errors->first("setting.{$setting['id']}")}}</label>
                                                    @endif
                                                @endif
                                            </div>
                                            <div class="col-sm-1">
                                                <a href="javascript:void(0);" class="btn btn-default" id="a-setting-update-{{$setting['id']}}" data-setting_id="{{$setting['id']}}" setting_id="{{$setting['id']}}">更新</a>
                                            </div>
                                        </div>
                                    @endforeach
                                </div>
                            @endforeach
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('appendJS')
    <script src="{{asset('/back/validate/jquery.validate.min.js')}}"></script>
    <script src="{{asset('/back/validate/additional-methods.min.js')}}"></script>
    <script src="{{asset('/back/validate/localization/messages_zh.min.js')}}"></script>
    <script>
		$(function(){
			/***ajax提交更新***/
			// 找到所有的id属性以a-setting-update-开头的a元素
			// 绑定click事件
			$('a[id^=a-setting-update-]').click(function(evt) {
				// 找到对应的表单元素
				var setting_id = $(this).data('setting_id');
				// 当前setting_id对应表单元素
				var settingElement = $('[name^="setting['+setting_id +']"]').eq(0);
                //ajax更新
				var url = '{{route('setting.ajax.set')}}';
				var data = {
					setting_id: setting_id,
					value: settingElement.val(),
					_token: '{{csrf_token()}}'
				};
				$.ajax({
					type: "POST",
					url: url,
					dataType: 'json',
					data: data,
					statusCode: {422: function(AJAXRequest){
						//如果状态吗是422则表示验证失败
                        var errorMsg = AJAXRequest.responseJSON.value[0];
                        alert(errorMsg);
                    }},
					success: function(resp){
                        alert(resp.msg);
					},
                    error : function (AJAXRequest) {

                    }
			});
			});
		});
    </script>
@endsection